<?php
// 数据库设置脚本
header('Content-Type: text/html; charset=utf-8');

echo "<h2>数据库设置向导</h2>";

// 当前配置
$current_config = [
    'host' => 'localhost',
    'dbname' => 'ldw',
    'username' => 'ldw',
    'password' => 'ldwldwldw'
];

echo "<h3>当前配置</h3>";
echo "<ul>";
echo "<li>主机: {$current_config['host']}</li>";
echo "<li>数据库: {$current_config['dbname']}</li>";
echo "<li>用户名: {$current_config['username']}</li>";
echo "<li>密码: {$current_config['password']}</li>";
echo "</ul>";

// 测试当前配置
echo "<h3>测试当前配置</h3>";
try {
    $dsn = "mysql:host={$current_config['host']};charset=utf8mb4";
    $pdo = new PDO($dsn, $current_config['username'], $current_config['password'], [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
    
    echo "<p style='color: green;'>✅ 数据库连接成功！</p>";
    
    // 尝试创建数据库
    try {
        $pdo->exec("CREATE DATABASE IF NOT EXISTS `{$current_config['dbname']}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
        echo "<p style='color: green;'>✅ 数据库 '{$current_config['dbname']}' 创建/验证成功！</p>";
        
        // 使用数据库
        $pdo->exec("USE `{$current_config['dbname']}`");
        
        // 测试创建表
        $pdo->exec("CREATE TABLE IF NOT EXISTS test_table (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50))");
        echo "<p style='color: green;'>✅ 表创建权限正常！</p>";
        
        // 清理测试表
        $pdo->exec("DROP TABLE IF EXISTS test_table");
        
        echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px; margin: 20px 0;'>";
        echo "<h4>✅ 数据库配置完全正常！</h4>";
        echo "<p>您的系统应该可以正常工作了。如果仍有问题，请检查以下几点：</p>";
        echo "<ul>";
        echo "<li>确保MySQL服务正在运行</li>";
        echo "<li>确保用户 '{$current_config['username']}' 有足够的权限</li>";
        echo "<li>检查防火墙设置</li>";
        echo "</ul>";
        echo "</div>";
        
    } catch (PDOException $e) {
        echo "<p style='color: red;'>❌ 数据库操作失败: " . $e->getMessage() . "</p>";
        
        echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px; margin: 20px 0;'>";
        echo "<h4>可能的解决方案：</h4>";
        echo "<ol>";
        echo "<li>使用root用户创建数据库和用户</li>";
        echo "<li>给用户 '{$current_config['username']}' 分配足够的权限</li>";
        echo "<li>或者修改database.php使用root用户</li>";
        echo "</ol>";
        echo "</div>";
    }
    
} catch (PDOException $e) {
    echo "<p style='color: red;'>❌ 数据库连接失败: " . $e->getMessage() . "</p>";
    
    echo "<h3>尝试使用root用户连接</h3>";
    
    // 尝试常见的root配置
    $root_configs = [
        ['username' => 'root', 'password' => 'root'],
        ['username' => 'root', 'password' => ''],
        ['username' => 'root', 'password' => '123456']
    ];
    
    foreach ($root_configs as $config) {
        echo "<h4>测试 root 用户 (密码: '" . ($config['password'] ?: '无') . "')</h4>";
        
        try {
            $dsn = "mysql:host={$current_config['host']};charset=utf8mb4";
            $pdo = new PDO($dsn, $config['username'], $config['password'], [
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            ]);
            
            echo "<p style='color: green;'>✅ Root连接成功！</p>";
            
            // 创建数据库和用户
            try {
                $pdo->exec("CREATE DATABASE IF NOT EXISTS `{$current_config['dbname']}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
                echo "<p style='color: green;'>✅ 数据库创建成功！</p>";
                
                // 创建用户并授权
                $pdo->exec("CREATE USER IF NOT EXISTS '{$current_config['username']}'@'localhost' IDENTIFIED BY '{$current_config['password']}'");
                $pdo->exec("GRANT ALL PRIVILEGES ON `{$current_config['dbname']}`.* TO '{$current_config['username']}'@'localhost'");
                $pdo->exec("FLUSH PRIVILEGES");
                
                echo "<p style='color: green;'>✅ 用户创建和授权成功！</p>";
                
                echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px; margin: 20px 0;'>";
                echo "<h4>✅ 数据库设置完成！</h4>";
                echo "<p>现在您的系统应该可以正常工作了。</p>";
                echo "</div>";
                
                break;
                
            } catch (PDOException $e) {
                echo "<p style='color: orange;'>⚠️ 用户创建失败: " . $e->getMessage() . "</p>";
            }
            
        } catch (PDOException $e) {
            echo "<p style='color: red;'>❌ Root连接失败: " . $e->getMessage() . "</p>";
        }
    }
}

echo "<hr>";
echo "<h3>手动解决方案</h3>";
echo "<p>如果自动设置失败，请手动执行以下SQL命令：</p>";
echo "<pre style='background: #f8f9fa; padding: 15px; border-radius: 5px;'>";
echo "-- 创建数据库\n";
echo "CREATE DATABASE IF NOT EXISTS `{$current_config['dbname']}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n\n";
echo "-- 创建用户\n";
echo "CREATE USER IF NOT EXISTS '{$current_config['username']}'@'localhost' IDENTIFIED BY '{$current_config['password']}';\n\n";
echo "-- 授权\n";
echo "GRANT ALL PRIVILEGES ON `{$current_config['dbname']}`.* TO '{$current_config['username']}'@'localhost';\n";
echo "FLUSH PRIVILEGES;";
echo "</pre>";
?>